package com.example.testerclientapp;
import com.example.testerclientapp.R;


import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
//import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class MainActivity extends Activity 
{
	EditText Email,pwd;
    Button signin,sign_up;
    
    private static final String SOAP_ACTION = "http://spartansparking.org/login"; 
    private static final String METHOD_NAME =  "login";       
    private static final String NAMESPACE =    "http://spartansparking.org/";    
    private static final String URL = "http://spartanparking.azurewebsites.net/parkingservice.asmx";   
    
    private AndroidHttpTransport mAht;
    public  SoapSerializationEnvelope mSoapEnvelope;
	//private View visitor;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ///////////// STUDENT OR VISITOR ///////////
        
        signin=(Button)findViewById(R.id.sign_u);
        sign_up = (Button)findViewById(R.id.signup);
        
        sign_up.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		Intent i=new Intent(MainActivity.this, SignupActivity.class);
        		startActivity(i);
        	
        	}
        }
        );
    
        
        signin.setOnClickListener(new View.OnClickListener() {
        	@Override
            public void onClick(View v) 
        	{
        		  try
        	        {
        			  	//Init EditText object`
        		        Email = (EditText)findViewById(R.id.u_type);
        		        pwd = (EditText)findViewById(R.id.editText2);
        		        		        		
        		        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        		        request.addProperty("email", Email.getText().toString());
        		        request.addProperty("password", pwd.getText().toString());
        		        
        		        
        		        
        		        mSoapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        		        mSoapEnvelope.dotNet = true;
        		        mSoapEnvelope.setOutputSoapObject(request);
        		        
        		        mAht = new AndroidHttpTransport(URL);
        	        	mAht.call(SOAP_ACTION, mSoapEnvelope);   //in-out parameter: soapEnvelope, which means the response back will be stored in this var soapEnvelope
        	        	
        	        	
        	        	 if (mSoapEnvelope.bodyIn instanceof SoapFault)
        	        	 {
        	        		 String str= ((SoapFault) mSoapEnvelope.bodyIn).faultstring;
        	        		 createAndShowDialog(str, "Login Error");
        	        		 
        	        	}
        	        	else 
        	        		 {
        	              		 SoapPrimitive resultsRequestSOAP = (SoapPrimitive)mSoapEnvelope.getResponse();
        		     
        		        		 if (resultsRequestSOAP.toString().equals("true"))
        		        		 {
        		        			 //createAndShowDialog(resultsRequestSOAP.toString(), "Server Response");
        		        			 Toast.makeText(getApplicationContext()," Successfully logged in", Toast.LENGTH_LONG).show();
        		        			 Intent reservationSpotActivity = new Intent(MainActivity.this, ReservationSpotActivity.class);
        		        			 //Intent in = new Intent(MainActivity.this, ReservationSpotActivity.class);
        	        	   			 reservationSpotActivity.putExtra("email", Email.getText().toString());
        		        			 startActivity(reservationSpotActivity);
        		        		 }
        		        		 else
        		        		 	{
        		        			 	// return "false" means no user found in database
        		        			 	//createAndShowDialog(resultsRequestSOAP.toString(), "Server Response");
        		        			    Toast.makeText(getApplicationContext()," Username and/or Password are incorrect", Toast.LENGTH_LONG).show();
        		        		 	}
        	        		 }
        	       }
        	       
        	       
        	        catch (Exception ex)
        	        {
        	        	ex.printStackTrace();
        	           	Toast.makeText(getBaseContext(), "Error: "+ex.getMessage(), Toast.LENGTH_LONG).show();
        	        }
             }  // end onclick signin
        }); //end onclick listener

        
        
        
        	}//end onCreate
   
    
    public void createAndShowDialog(String message, String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setMessage(message);
		builder.setTitle(title);
		builder.create().show();
	}
    
}//end class
    
    
    /*********** public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ///////////// STUDENT OR VISITOR ///////////
        
        signin=(Button)findViewById(R.id.sign_u);
        sign_up = (Button)findViewById(R.id.signup);
        
        sign_up.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		Intent i=new Intent(MainActivity.this, SignupActivity.class);
        		startActivity(i);
        	
        	}
        }
        );
    
        
        signin.setOnClickListener(new View.OnClickListener() {
        	@Override
            public void onClick(View v) 
        	{
        		  try
        	        {
        			  	//Init EditText object`
        		        email = (EditText)findViewById(R.id.editText1);
        		        pwd = (EditText)findViewById(R.id.editText2);
        		        
        		        
 //if ((email.getText().toString().length() != 0) && (pwd.getText().toString().length()!=0))
        
  //{
        		        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        		        request.addProperty("email", email.getText().toString());
        		        request.addProperty("password", pwd.getText().toString());
        		        
        		        mSoapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        		        mSoapEnvelope.dotNet = true;
        		        mSoapEnvelope.setOutputSoapObject(request);
        		        
        		        mAht = new AndroidHttpTransport(URL);
        	        	mAht.call(SOAP_ACTION, mSoapEnvelope);   //in-out parameter: soapEnvelope, which means the response back will be stored in this var soapEnvelope
        	        	
        	        	
        	        	 if (mSoapEnvelope.bodyIn instanceof SoapFault)
        	        	 {
        	        		 String str= ((SoapFault) mSoapEnvelope.bodyIn).faultstring;
        	        		 createAndShowDialog(str, "Login Error");
        	        		 
        	        	}
        	        	else 
        	        		 {
        	              		 SoapPrimitive resultsRequestSOAP = (SoapPrimitive)mSoapEnvelope.getResponse();
        		     
        		        		 if (resultsRequestSOAP.toString().equals("true"))
        		        		 {
        		        			 //createAndShowDialog(resultsRequestSOAP.toString(), "Server Response");
        		        			 Intent reservationSpotActivity = new Intent(MainActivity.this, ReservationSpotActivity.class);
        		        			 startActivity(reservationSpotActivity);
        		        		 }
        		        		 else
        		        		 	{
        		        			 	// return "false" means no user found in database
        		        			 	createAndShowDialog(resultsRequestSOAP.toString(), "Server Response");
        		        		 	}
        	        		 }
        	       
        	        }
        	            /* uncomment this after fixing *********/
        		        /*else
        		        {
        		        	Toast.makeText(getBaseContext(), "Error: Username or Password should not be empty"+ email.getText().toString(), Toast.LENGTH_LONG).show();
        		        	Intent ig=new Intent(MainActivity.this, MainActivity.class);
        	        		startActivity(ig);
        		        
        		        
        		        } */
        	        //}
        	       
        	      /*********  catch (Exception ex)
        	        {
        	        	ex.printStackTrace();
        	           	Toast.makeText(getBaseContext(), "Error: "+ex.getMessage(), Toast.LENGTH_LONG).show();
        	        }
             }  // end onclick signin
        }); //end onclick listener

        
        
        
        	}//end onCreate
   
    
    public void createAndShowDialog(String message, String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setMessage(message);
		builder.setTitle(title);
		builder.create().show();
	}
    
}//end class
    
    
    /*********** public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ///////////// STUDENT OR VISITOR ///////////
        
        signin=(Button)findViewById(R.id.sign_u);
        sign_up = (Button)findViewById(R.id.signup);
        
        sign_up.setOnClickListener(new View.OnClickListener()
        {
        	@Override
        	public void onClick(View v)
        	{
        		Intent i=new Intent(MainActivity.this, SignupActivity.class);
        		startActivity(i);
        	
        	}
        }
        );
    
        
        signin.setOnClickListener(new View.OnClickListener() {
        	@Override
            public void onClick(View v) 
        	{
        		  try
        	        {
        			  	//Init EditText object`
        		        email = (EditText)findViewById(R.id.editText1);
        		        pwd = (EditText)findViewById(R.id.editText2);
        		        
        		        
 //if ((email.getText().toString().length() != 0) && (pwd.getText().toString().length()!=0))
        
  //{
        		        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        		        request.addProperty("email", email.getText().toString());
        		        request.addProperty("password", pwd.getText().toString());
        		        
        		        mSoapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        		        mSoapEnvelope.dotNet = true;
        		        mSoapEnvelope.setOutputSoapObject(request);
        		        
        		        mAht = new AndroidHttpTransport(URL);
        	        	mAht.call(SOAP_ACTION, mSoapEnvelope);   //in-out parameter: soapEnvelope, which means the response back will be stored in this var soapEnvelope
        	        	
        	        	
        	        	 if (mSoapEnvelope.bodyIn instanceof SoapFault)
        	        	 {
        	        		 String str= ((SoapFault) mSoapEnvelope.bodyIn).faultstring;
        	        		 createAndShowDialog(str, "Login Error");
        	        		 
        	        	}
        	        	else 
        	        		 {
        	              		 SoapPrimitive resultsRequestSOAP = (SoapPrimitive)mSoapEnvelope.getResponse();
        		     
        		        		 if (resultsRequestSOAP.toString().equals("true"))
        		        		 {
        		        			 //createAndShowDialog(resultsRequestSOAP.toString(), "Server Response");
        		        			 Intent reservationSpotActivity = new Intent(MainActivity.this, ReservationSpotActivity.class);
        		        			 startActivity(reservationSpotActivity);
        		        		 }
        		        		 else
        		        		 	{
        		        			 	// return "false" means no user found in database
        		        			 	createAndShowDialog(resultsRequestSOAP.toString(), "Server Response");
        		        		 	}
        	        		 }
        	       
        	        }
        	            /* uncomment this after fixing *********/
        		        /*else
        		        {
        		        	Toast.makeText(getBaseContext(), "Error: Username or Password should not be empty"+ email.getText().toString(), Toast.LENGTH_LONG).show();
        		        	Intent ig=new Intent(MainActivity.this, MainActivity.class);
        	        		startActivity(ig);
        		        
        		        
        		        } */
        	        //}
        	       
        	      /*******  catch (Exception ex)
        	        {
        	        	ex.printStackTrace();
        	           	Toast.makeText(getBaseContext(), "Error: "+ex.getMessage(), Toast.LENGTH_LONG).show();
        	        }
             }  // end onclick signin
        }); //end onclick listener

        
        
        
        	}//end onCreate
   
    
    public void createAndShowDialog(String message, String title) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setMessage(message);
		builder.setTitle(title);
		builder.create().show();
	}
    
}//end class



************/





